package cn.cpy.sms.login.utils;

import cn.cpy.sms.login.common.enumerator.ServiceCode;
import cn.cpy.sms.login.exception.ServiceException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
public class SmsUtil {

    @Value("${tencent.secret-id}")
    private String SecretId;
    @Value("${tencent.secret-key}")
    private String SecretKey;
    @Value("${tencent.sms-sdk-app-id}")
    private String SmsSdkAppId;
    @Value("${tencent.sign-name}")
    private String SignName;
    @Value("${tencent.template-id}")
    private String TemplateId;

    public void sendSMS(String phone,String code){
        try{
            Credential cred = new Credential(SecretId, SecretKey);
            //System.out.println("******************"+SecretId);
            // 实例化⼀个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化⼀个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            // 实例化⼀个请求对象,每个接⼝都会对应⼀个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {phone};
            req.setPhoneNumberSet(phoneNumberSet1);
            //短信的SdkAppId,在短信控制台添加应⽤后⽣成
            req.setSmsSdkAppId(SmsSdkAppId);
            //短信签名,填写的是签名内容不是签名ID
            String signName = new String(SignName.getBytes("ISO-8859-1"), "UTF-8");
//            log.info("签名:"+signName);
            req.setSignName(signName);
            //短信模板的ID
            req.setTemplateId(TemplateId);
            String[] templateParamSet1 = {code};
            req.setTemplateParamSet(templateParamSet1);
            // 返回的resp是⼀个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包 57
            log.info(SendSmsResponse.toJsonString(resp));
            if(resp.getSendStatusSet()[0].getCode().startsWith("FailedOperation")){
                String errorMsg = resp.getSendStatusSet()[0].getCode().replace("FailedOperation.","");
                String serviceInfo = "";
                switch(errorMsg){
                    case "SignatureIncorrectOrUnapproved":
                        serviceInfo = "签名错误，请检查!";
                        break;
                }
                throw new ServiceException(ServiceCode.ERROR_TENCENT_MSM,serviceInfo);
            }

        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
        }
        catch(UnsupportedEncodingException ue){
            ue.printStackTrace();
        }
    }
}
